package gsd.gaudi.server.replayer.exception;

import gsd.gaudi.server.replayer.GaudiEventReplayer;

import java.awt.Frame;
import java.lang.Thread.UncaughtExceptionHandler;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class GuiUncaughtExceptionHandler implements  UncaughtExceptionHandler{

	private GaudiEventReplayer ger;
	
	public GuiUncaughtExceptionHandler(GaudiEventReplayer ger){
		this.ger = ger;
	}
	
	public void uncaughtException(Thread t, Throwable e) {
		// Here you should have a more robust, permanent record of problems
		//JOptionPane.showMessageDialog(findActiveFrame(), e.toString(), "Exception Occurred", JOptionPane.OK_OPTION);
		e.printStackTrace();
		System.out.println("[GaudiServer]-Finished replay");
		ger.printLastMetrics();
	}

	private Frame findActiveFrame() {
		Frame[] frames = JFrame.getFrames();
		for (int i = 0; i < frames.length; i++) {
			if (frames[i].isVisible()) {
				return frames[i];
			}
		}
		return null;
	}
}
